<template>
  <div class="login">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="img_box">
        <img src="../assets/logo.jpg" alt />
      </div>
      <!-- 表单区域 -->
      <!-- " -->
      <!--   -->
      <el-form class="login_form" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item prop="user">
          <el-input v-model="loginForm.user" prefix-icon=" iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon=" iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login()">注册</el-button>
          <el-button type="primary" @click="logins()">登录</el-button>
          <el-button type="info" @click="loginFormRef">重置</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>
<script>
import axi from '../utils/text.js'
export default {
  data() {
    return {
      loginForm: {
        user: '',
        password: ''
      },
      list: [],
      loginFormRules: {
        user: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在3~10个字符之间',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在3~10个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getlist()
  },
  methods: {
    loginFormRef() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) {
          return
        }
        this.$http.post('/getuser', this.loginForm).then(res => {
          if (res.data.err_code === 200) {
            this.$message.success('抱歉此用户名已注册')
          } else {
            this.$http.post('/user', this.loginForm).then(res => {
              if (res.data.err_code === 0) {
                window.sessionStorage.setItem('token', res.data.jwt)
                this.$message.success('注册成功 正在跳转首页请稍后')
                this.getAdd()
                //  this.$message.success('正在跳转首页请稍后')
                setTimeout(() => {
                  this.$router.push('/home')
                }, 1500)
              }
            })
          }
        })
      })
    },
    async logins() {
      const { err, jwt, message } = await axi.getLogin(this.loginForm)
      if (err === -1) return this.$message.warning('账号或密码不正确')
      window.sessionStorage.setItem('token', jwt)
      this.getAdd()
      this.$message.success(message)
      this.$router.push('/home')
      // this.$http.post('/users/login', this.loginForm).then(res => {
      //   if (res.data.err_code !== 1) {
      //     this.$message.success('登录成功 正在跳转首页请稍后')
      //     //   this.$message.success('正在跳转首页请稍后')
      //     window.sessionStorage.setItem('token', res.data.jwt)
      //     this.getAdd()
      //     setTimeout(() => {
      //       this.$router.push('/home')
      //     }, 1500)
      //   } else {
      //     this.$message.warning('账号或密码不正确')
      //   }
      // })
    },
    getAdd() {
      const t = window.sessionStorage.getItem('token')
      const data = {
        token: window.sessionStorage.getItem('token')
      }
      console.log(data)
      axi.gettoken({ token: t }).then(res => {
        const u = res.data.message.username
        console.log(u)
        const da = {
          user: JSON.stringify(u)
        }
        this.$http.post('/user/admin', da).then(res => {
          console.log(res.data)
          let v
          const c = this.list.some(item => {
            if (item.userid === res.data.message[0].id) {
              v = item.userid
            }
            return item.userid === res.data.message[0].id
          })
          localStorage.setItem('userid', v)
          this.$store.commit('getstateuserid', v)
          if (c) {
            this.updatedEch(res.data.message)
          } else {
            this.getEch(res.data.message)
          }
        })
      })
    },
    getlist() {
      axi.getecharts().then(res => {
        this.list = res.data.message
      })
    },
    // 添加访问量
    getEch(data) {
      console.log(data, '添加的数据')
      const d = {
        userid: data[0].id,
        user: data[0].user,
        time: +new Date(),
        total: 1,
        datanum: 1
      }
      console.log(d)
      axi.addecharts(d).then(res => {
        console.log(res, '添加没有')
      })
    },
    // 修改访问量
    updatedEch(data) {
      const D = +new Date().getDate()
      const Y = +new Date().getMonth() + 1
      const t1 = `2020/${Y}/${D} 00:00:00`
      const t2 = +new Date(t1)
      const t = +new Date(1591459200000)
      const d = data[0]
      let c
      // new Date(Number(item.time)).getDate()>new Date(t2).getDate()&&new Date(Number(item.time)).getMonth()+1<new Date(t2).getMonth()+1
      this.list.forEach(item => {
        if (item.userid === d.id) {
          console.log(item.time > +new Date('2020/9/13'))
          //  console.log(new Date(Number(item.time)).getDate(),item.time,new Date(t2).getDate())
          console.log(item.time > t2)
          if (item.time > +new Date()) {
            c = item
            c.total = c.total + 1
            c.datanum = c.datanum + 1
            console.log('这里')
          } else {
            c = item
            c.total = c.total + 1
            c.datanum = 1
            c.time = t2 + 86400000
            console.log('还是这里')
          }
          // new Date(Number(c.time)).getDate()
          console.log(c, '要修改的数据',)
          axi.updatedechart(c).then(res => {
            console.log(c, '修改成功了')
          })
        } else {
          //   if(new Date(Number(item.time)).getDate()>new Date(t2).getDate()){
          //      c = item
          //   c.total = c.total + 1
          //   c.datanum=c.datanum+1
          //   console.log('这里')
          //  }else{
          //     c=item
          //    c.total=c.total+1
          //    c.datanum=0
          //    c.time=t2+86400000
          //    console.log('还是这里')
          //  }
          //  //new Date(Number(c.time)).getDate()
          //   console.log(c,'要修改的数据',)
          //   axi.updatedechart(c).then(res => {
          //     console.log(c, '修改成功了')
          //   })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  background: url('../assets/bgc.jpg');
  background-size: 100% 100%;
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border: 1px solid #ccc;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .img_box {
      width: 130px;
      height: 130px;
      border: 1px solid #ccc;
      border-radius: 50%;
      padding: 10px;
      position: absolute;
      box-shadow: 0 0 10px #ccc;
      left: 50%;
      top: -25%;
      transform: translate(-50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 30px;
      box-sizing: border-box;
    }
    .btns {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
